package com.xiaoqing.framework.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoqing.framework.api.AccountMapper;
import com.xiaoqing.framework.bean.Account;
import com.xiaoqing.framework.common.exception.BizException;
import com.xiaoqing.framework.common.kits.Snowflake;
import com.xiaoqing.framework.response.account.LoginResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


/**
 * @author: <a href="get_xiaobo@163.com">Josh_Xu</a>
 * @description:
 * @create: 2020-06-20 2:25
 **/
@Service
@Slf4j
public class LoginService extends ServiceImpl<AccountMapper, Account> {

    @Autowired
    private AccountMapper accountMapper;




    public LoginResponse login(String accountName, String accountPwd) {
        Account account = this.accountMapper.selectOne(new QueryWrapper<Account>()
                .eq("account_name", accountName)
                .eq("password", accountPwd));
        Optional.ofNullable(account).orElseThrow(()->new BizException("用户名或者密码错误!"));
        LoginResponse response = new LoginResponse();
        response.setToken("new token");
        return response;
    }

    public void register(String accountName, String accountPwd) {
        Account account = Account.builder()
                .accountName(accountName)
                .password(accountPwd)
                .accountId(Snowflake.generateId()).build();
        this.accountMapper.insert(account);
    }
}
